package com.bluefish.bltuan.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.annotation.RequireLogin;
import com.bluefish.bltuan.annotation.RequireRight;
import com.bluefish.bltuan.module.shop.Delivery;
import com.bluefish.bltuan.module.shop.Order;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.service.OrderService;
import com.bluefish.bltuan.service.ShopService;
import com.bluefish.bltuan.tools.ActionUtil;
import com.bluefish.bltuan.tools.shop.AccordType;
import com.bluefish.bltuan.tools.user.UserRight;

@RequireLogin
@Results({
	@Result(name = "show", type = "dispatcher", location = "/page/order-show.jsp"),
	@Result(name = "message", type = "dispatcher", location = "/page/order-message.jsp"),
	@Result(name = "express", type = "dispatcher", location = "/page/order-express.jsp"),
	@Result(name = "toShow", type = "redirect", location = "/order/show", params = {"orderId","${order.id}"})
})
public class OrderAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TuanGou tuanGou;
	
	private Order order;
	
	private Delivery delivery;
	
	private List<Order> orders;
	
	private ShopService shopService;

	private OrderService orderService; 

	private Integer orderId;
	
	public Integer getOrderId() {
		return orderId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public Delivery getDelivery() {
		return delivery;
	}

	public void setDelivery(Delivery delivery) {
		this.delivery = delivery;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public TuanGou getTuanGou() {
		return tuanGou;
	}

	public void setTuanGou(TuanGou tuanGou) {
		this.tuanGou = tuanGou;
	}

	@Override
	public HttpHeaders index() {
		User user = (User) getCurrentUser();
		this.orders = this.orderService.getOrdersByUser(user, getPageIndex());
		return new DefaultHttpHeaders(INDEX).disableCaching();
	}

	@Override
	public HttpHeaders show() {
		this.order = this.orderService.findById(this.orderId);
		return new DefaultHttpHeaders(SHOW).disableCaching();
	}

	/*@Before(include={"create"})
	public TuanGou currentTuangou(){
		if(getId() != null)
			return this.shopService.findById(getIntegerId());
		return null;
	}*/
	
	/**
	 * 购买商品，调用支付宝接口
	 */
	@Override
	public HttpHeaders create() {
		initTuanGou();
		messageWay(this.order);
		this.orderService.save(this.order);
		return new DefaultHttpHeaders(SUCCESS).disableCaching();
	}

	public HttpHeaders createOfMes() {
		initTuanGou();
		messageWay(this.order);
		this.orderService.save(this.order);
		return new DefaultHttpHeaders("toShow").disableCaching();
	}
	
	private void initTuanGou(){
		TuanGou tuanGou = this.shopService.findById(getIntegerId());
		this.order.setTuangou(tuanGou);
	}
	
	public HttpHeaders createOfExs() {
		initTuanGou();
		expressWay(this.order);
		this.orderService.save(this.order);
		return new DefaultHttpHeaders("toShow").disableCaching();
	}
	
	/**
	 * 出售方式为短消息
	 * @param order
	 */
	private void messageWay(Order order){
		String phone = ActionUtil.getRequestParameter("phone");
		User currentUser = (User)getCurrentUser();
		currentUser.setPhone(phone);
		order.setUser(currentUser);
	}
	
	/**
	 * 出售方式为物流
	 * @param order
	 */
	private void  expressWay(Order order){
		order.setDelivery(this.delivery);
		User currentUser = (User)getCurrentUser();
		order.setUser(currentUser);
	}
	
	
	
	/**
	 * 用户下订单
	 * @return
	 */
	@Override
	public HttpHeaders editNew() {
		return null;
	}


	@RequireRight(right = UserRight.NORMAL)
	public HttpHeaders buy() {
		this.tuanGou = this.shopService.findById(getIntegerId());
		if(this.tuanGou != null){
			if(this.tuanGou.isFinished())
				return new DefaultHttpHeaders("over").disableCaching();
			String forward = this.tuanGou.getAccordType() == AccordType.MESSAGE  ? "message" : "express";
			return new DefaultHttpHeaders(forward).disableCaching();
		}
		return null;
	}

	public HttpHeaders pay(){
		this.order = this.orderService.findById(getIntegerId());
		return new DefaultHttpHeaders("toShow").disableCaching();
	}
	
	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String destroy() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
